<?php

namespace app\middleware;

use app\admin\Utils\model\UtilLang;
use Illuminate\Support\Facades\Session;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;

class Lang implements MiddlewareInterface
{
    public function process(Request $request, callable $handler): Response
    {
        $session = $request->session();
        if($lang = $request->get('lang')){
            $langName = UtilLang::where(['code'=>$lang])->value('name');
            $session->set('lang',$lang);
            $session->set('langName',$langName);
        }
        $lang = $session->get('lang','en');
        locale($lang);
        return $handler($request);
        // TODO: Implement process() method.
    }
}